package com.sinoglobal.dumping.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewStub;
import android.widget.AdapterView;
import android.widget.ListView;

import com.sinoglobal.dumping.R;
import com.sinoglobal.dumping.adapter.Dumpling_MakeCardsAdapter;
import com.sinoglobal.dumping.api.Dumpling_RemoteImpl;
import com.sinoglobal.dumping.base.Dumpling_SinoAsyncTask;
import com.sinoglobal.dumping.base.Dumpling_SinoBaseActivity;
import com.sinoglobal.dumping.bean.Dumpling_CardVo;
import com.sinoglobal.wallet.view.pulltorefresh.WPullToRefreshView;

import java.util.ArrayList;

/**
 * 我包 贺卡
 */
public class Dumpling_MakeCardListActivity extends Dumpling_SinoBaseActivity implements WPullToRefreshView.OnFooterRefreshListener, WPullToRefreshView.OnHeaderRefreshListener, AdapterView.OnItemClickListener {

    private ListView makeCards;
    private int page = 1;
    private Dumpling_MakeCardsAdapter cardsAdapter;
    private WPullToRefreshView pullToRefreshView;
    private ViewStub noDataView;
    private boolean isShow = true;
    private int itemNum;
    private int totalityPage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dumpling_activity_make_card_list);
        mTemplateTitleText.setText("包贺卡");
        View view = getView(R.id.title_right_btn_share);
        hideView(mTemplateRightText, mTemplateRightImg, view);
        pullToRefreshView = (WPullToRefreshView) findViewById(R.id.dumpling_make_card_pulltorefresh);
        pullToRefreshView.setOnFooterRefreshListener(this);
        pullToRefreshView.setOnHeaderRefreshListener(this);
        makeCards = (ListView) findViewById(R.id.dumpling_make_card_list);
        noDataView = (ViewStub) findViewById(R.id.dumpling_make_card_nodata);
        itemNum = getIntent().getIntExtra("cardNumber", 10);
        totalityPage=itemNum%20>0?itemNum/20+1:itemNum/20;
        if (itemNum>0){
            getListInfo(page);
        }else {
            noDataView.inflate();
        }
        makeCards.setOnItemClickListener(this);
    }

    private void getListInfo(final int page) {
        new Dumpling_SinoAsyncTask<Void, Void, ArrayList<Dumpling_CardVo>>(Dumpling_MakeCardListActivity.this) {
            @Override
            public ArrayList<Dumpling_CardVo> before(Void... params) throws Exception {
                return Dumpling_RemoteImpl.getInstance().putCardPage(page);
            }

            @Override
            public void after(ArrayList<Dumpling_CardVo> dumpling_cardVos) {
                if (dumpling_cardVos != null && page == 1) {
                    if (dumpling_cardVos.size() > 0) {
                        cardsAdapter = new Dumpling_MakeCardsAdapter(dumpling_cardVos, Dumpling_MakeCardListActivity.this);
                        makeCards.setAdapter(cardsAdapter);
                        pullToRefreshView.onHeaderRefreshComplete();
                        isShow = false;
                    }else {
                        showNoDataView();
                    }
                } else if (page > 1) {
                    pullToRefreshView.onFooterRefreshComplete();
                    if (dumpling_cardVos.size() > 0) {
                        cardsAdapter.addMore(dumpling_cardVos);
                    }
                }
            }

            @Override
            public void exception() {
                pullToRefreshView.onFooterRefreshComplete();
                pullToRefreshView.onHeaderRefreshComplete();
                showNoDataView();
            }
        }.execute();
    }

    public void showNoDataView() {
        if (isShow) {
            noDataView.inflate();
            isShow = false;
        }
    }

    @Override
    public void onFooterRefresh(WPullToRefreshView view) {
        page++;
        if (page<=totalityPage){
            getListInfo(page);
        }else {
            showToast("没有更多数据");
            pullToRefreshView.onFooterRefreshComplete();
        }
    }

    @Override
    public void onHeaderRefresh(WPullToRefreshView view) {
        page=1;
        getListInfo(page);
    }

    @Override
    public void onAntiViolence(View view) {

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        if (3 != cardsAdapter.getCards().get(i).getStatus() && !TextUtils.isEmpty(cardsAdapter.getCards().get(i).getDumplingUserPutcardId())) {
            Intent intent = new Intent(this, Dumpling_MyCardDetailActivity.class);
            intent.putExtra("DumplingUserPutcardId", cardsAdapter.getCards().get(i).getDumplingUserPutcardId());
            intent.putExtra("fromType", 2);
            startActivity(intent);
        } else {
            showToast("参数错误");
        }
    }
}
